package arm.attestation.bean;

import arm.db.tools.DBManager;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class ReportBean
{
    private ObiektBean parent;

    public ReportBean(ObiektBean parent) {
        this.parent = parent;
    }

    public String toPage()
    {
        return "viewReport";
    }

    public String back ()
    {
        return "viewObiektTable" ;
    }

    public ObiektBean getParent() {
        return parent;
    }

    public List<Post> getList()
    {
        List<Post> result = new ArrayList<Post>();
        Long objId = parent.getObiekt().getId();
        String sql =
                "SELECT" +
                        " DISTINCT" +
                        " m.key_workplace," +
                        " MAX(m.number_workplace) number_workplace," +
                        " dp.id," +
                        " dp.name" +
                        " FROM metric m " +
                        " INNER JOIN dic_post dp" +
                        " ON dp.id = m.dic_post_id" +
                        " WHERE m.obiekt_id="+ objId +
                        " GROUP BY m.key_workplace, dp.id, dp.name";
        List postList = DBManager.readSQLList(sql);
        if (postList != null && postList.size() > 0)
        {
            Iterator it = postList.iterator();
            while (it.hasNext()) {
                Object[] obj = (Object[]) it.next();
                Integer keyWorkplace = ((BigInteger) obj[0]).intValue();
                Integer numberWorkplace = ((BigInteger) obj[1]).intValue();
                Long id = ((BigInteger) obj[2]).longValue();
                String name = (String)obj[3];
                Post post = new Post(id, name, keyWorkplace, numberWorkplace);
                sql =   "SELECT" +
                        " m.key_workplace key_workplace," +
                        " MAX(m.number_workplace) number_workplace," +
                        " dp.name dic_post_name," +
                        " dmt.name || ', ' || dmt.ed_izm dic_metric_type_name," +
                        " AVG(m.value) mvalue," +
                        " dn.max_dimension normi_max_dimension," +
                        " dn.min_dimension normi_min_dimension" +
                        " FROM metric m" +
                        " INNER JOIN dic_post dp" +
                        " ON dp.id = m.dic_post_id" +
                        " INNER JOIN dic_metric_type dmt" +
                        " ON dmt.id = m.dic_metric_type_id" +
                        " LEFT JOIN dic_normi dn" +
                        " ON dn.dic_metric_type_id = dmt.id AND dn.dic_category_id =  m.dic_category_id" +
                        " WHERE m.obiekt_id="+objId+
                        " AND m.dic_post_id="+post.getId()+
                        " AND m.key_workplace="+post.getKeyWorkplace()+
                        " GROUP BY m.key_workplace, dp.name, dmt.name, dmt.ed_izm, dn.max_dimension, dn.min_dimension" +
                        " ORDER BY m.key_workplace, dmt.name ";
                post.setMetricList(new ArrayList<PostMetric>());
                List metricList = DBManager.readSQLList(sql);
                if (metricList != null && metricList.size() > 0)
                {
                    Iterator itm = metricList.iterator();
                    while (itm.hasNext()) {
                        Object[] objm = (Object[]) itm.next();
                        keyWorkplace = objm[0] != null ? ((BigInteger) objm[0]).intValue() : 0;
                        numberWorkplace = objm[1] != null ? ((BigInteger) objm[1]).intValue() : 0;
                        String dicPostName = (String) objm[2];
                        String dicMetricTypeName = (String) objm[3];
                        Double value = objm[4] != null ? ((BigDecimal) objm[4]).doubleValue() : 0d;
                        Double maxDimension = objm[5] != null ? ((BigInteger) objm[5]).doubleValue() : 0d;
                        Double minDimension = objm[6] != null ? ((BigInteger) objm[6]).doubleValue() : 0d;
                        PostMetric metric = new PostMetric(keyWorkplace,numberWorkplace,dicPostName,dicMetricTypeName,value, maxDimension, minDimension);
                        post.getMetricList().add(metric);
                    }
                }
                result.add(post);
            }
        }
        return result;
    }
}
